package 力扣日常刷题.木22一月.第09天0117;

/**
 * @author 帅小伙
 * @date 2022/1/17
 * @description
 */
public class Demo03动态规划中等买卖股票的最佳时机 {


    public int maxProfit(int[] prices) {
        int ans = 0;
        int min = prices[0];
        int lastMax = prices[0];
        for (int i = 1; i < prices.length; i++) {
            if(prices[i] > lastMax) lastMax = prices[i];
            else {
                ans += (lastMax - min);
                min = lastMax = prices[i];
            }
        }
        ans +=(lastMax - min);
        return ans;
    }
}
